package com.bulletelc.apollopre.module.main.center.afterSale;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.entity.ServiceOrderBean;
import com.bulletelc.apollopre.entity.ServiceOrderGoodBean;
import com.bulletelc.apollopre.module.main.center.afterSale.detail.ServiceOrderDetailActivity;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by CX on 2018/1/26.
 */

public class ServiceOrderAdapter extends BaseAdapter implements View.OnClickListener{

    private List<ServiceOrderBean> sobList;
    private Context context;
    private LayoutInflater inflater;

    public ServiceOrderAdapter(Context context, List<ServiceOrderBean> sob) {
        this.context = context;
        this.sobList = sob;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return sobList.size();
    }

    @Override
    public Object getItem(int i) {
        return sobList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ServiceOrderBean sob = sobList.get(i);
        ViewHolder holder;
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.service_order_list_parent, null);

        holder.tvServiceNo = view.findViewById(R.id.tv_service_no);
        holder.btnCheck = view.findViewById(R.id.btn_check);

        holder.tvServiceNo.setText("服务单号：" + sob.getCode());
        holder.llGoods = view.findViewById(R.id.ll_layout_goods);
        holder.btnCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ServiceOrderDetailActivity.class);
                intent.putExtra("sodId", sob.getId());
                context.startActivity(intent);
            }
        });

//        view.setTag(holder);

        List<ServiceOrderGoodBean> sogList = sob.getServiceOrderGoodBeans();
        if (sogList != null) {
            for (int n=0;n<sogList.size();n++) {
                View[] views = new View[sogList.size()];
                views[n] = inflater.inflate(R.layout.service_order_list_item, null);
                ImageView ivImg = views[n].findViewById(R.id.iv_good_img);
                TextView tvName = views[n].findViewById(R.id.tv_good_name);
                TextView tvPrice = views[n].findViewById(R.id.tv_good_price);
                TextView tvCount = views[n].findViewById(R.id.tv_good_count);
                TextView tvSku = views[n].findViewById(R.id.tv_good_sku);
                TextView tvTotal = views[n].findViewById(R.id.tv_good_total_price);
                Glide.with(context).load(sogList.get(n).getLogo_url()).into(ivImg);
                tvName.setText(sogList.get(n).getProduct_name());
                tvPrice.setText("¥" + sogList.get(n).getOrigin_price());
                tvCount.setText("x"+ sogList.get(n).getQuantity());
                tvSku.setText(sogList.get(n).getProduct_sku_title());
                tvTotal.setText("退款金额：¥" + sogList.get(n).getReturn_price());
                holder.llGoods.addView(views[n]);
            }
        }

        return view;
    }

    @Override
    public void onClick(View view) {
        final int position = (int) view.getTag();
        switch (view.getId()) {
            case R.id.btn_check:

                break;
        }
    }

    class ViewHolder {
        TextView tvServiceNo, tvStatus;
        Button btnCheck;
        LinearLayout llGoods;
    }

}
